package org.hackystat.devcathlon.ui.page.profile.manage;

import java.util.Arrays;
import java.util.List;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.DevcathlonBasePage;
import org.hackystat.devcathlon.ui.page.profile.ProfilePage;
import org.hackystat.devcathlon.ui.page.profile.ProfileSession;

/**
 * Implements the Manage Profile page, which enables a user to edit and update personal details.
 * @author Anthony Du, Scheller Sanchez
 *
 */
public class ProfileManagePage extends DevcathlonBasePage {
  
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  /** The session state for this page. */
  private ProfileSession session = DevcathlonSession.get().getProfileSession(); 
  /** The user's first name. */
  protected String firstName = "";
  /** The user's last name. */
  protected String lastName = "";
  protected String screenName = "";
  protected String email = "";
  protected String bios = "";
  protected String location = "";
  protected String contact = "";
  protected String gender = "";
  //private static final List GENDER = Arrays.asList(new String[] { "Male", "Female" });

  
  /**
   * Creates the Profile page.  
   * This page contains a form to edit selected information of a user. 
   */
  public ProfileManagePage () {
    session.setProfile(DevcathlonSession.get().getEmail());
    org.hackystat.devcathlon.engine.profile.Profile profile = session.getProfile();
    firstName = profile.getFirstName();
    lastName = profile.getLastName();
    screenName = profile.getScreenName();
    email = profile.getEmails().toString();
    bios = profile.getBioInfo();
    location = profile.getLocation();
    contact = profile.getContact();
    gender = profile.getGender();

    List<String> genders = Arrays.asList("Male", "Female");
    
    // Set the body class attribute to section-1 so that the menubar is correct.
    add(new WebMarkupContainer("body") { 
      private static final long serialVersionUID = 1L;
      @Override
      public boolean isTransparentResolver() { 
        return true; 
      } 
      @Override
      protected void onComponentTag(ComponentTag tag) { 
        tag.put("class","section-1"); 
      }
    }); 
    
    // Create the form
    Form form = new Form("form");
    form.add(new TextField("screenName", new PropertyModel(this, "screenName")));
    form.add(new TextField("email", new PropertyModel(this, "email")));
    form.add(new TextField("firstName", new PropertyModel(this, "firstName")));
    form.add(new TextField("lastName", new PropertyModel(this, "lastName")));
    form.add(new TextArea("bios", new PropertyModel(this, "bios")));
    form.add(new TextField("location", new PropertyModel(this,"location")));
    form.add(new TextField("contact", new PropertyModel(this,"contact")));
    form.add(new RadioChoice("gender", new PropertyModel (this, "gender"), genders));
    form.add(new Button("Submit") {
      private static final long serialVersionUID = 1L;

      /** Display the page again, now with the updated values of field1 and field2. */
      @Override
      public void onSubmit() {   
        DevcathlonSession.get().getProfileSession().setBio(bios);
        DevcathlonSession.get().getProfileSession().setFirstName(firstName);
        DevcathlonSession.get().getProfileSession().setLastName(lastName);
        DevcathlonSession.get().getProfileSession().setContact(contact);
        DevcathlonSession.get().getProfileSession().setLocation(location);
        DevcathlonSession.get().getProfileSession().setScreenName(screenName);
        DevcathlonSession.get().getProfileSession().setGender(gender);
        setResponsePage(ProfilePage.class); 
      }
    });
    // Add the form to this page.
    add(form);
    
    
    // Make the tab corresponding to this page bold.
    get("ProfileManageLink").add(new SimpleAttributeModifier("class", "selected"));

  }
}
